package com.cos.lession73.slice;

import com.cos.lession73.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.multimodalinput.event.TouchEvent;

public class TouchAbilitySlice extends AbilitySlice {

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_touch);

        Button btn = (Button) findComponentById(ResourceTable.Id_cheat_btn);

        btn.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                //获取图片对象
                Image img = (Image) findComponentById(ResourceTable.Id_cheat_img);
                //touchEvent 触摸屏的事件 触摸中 离开 移动
                //获取当前事件中,属于触摸屏的哪一种事件
                int action = touchEvent.getAction();
                if (action==TouchEvent.PRIMARY_POINT_DOWN){
                    //指头按着不动
                    img.setImageAndDecodeBounds(ResourceTable.Media_rhzm);
                }else if(action== TouchEvent.PRIMARY_POINT_UP){
                    //指头离开
                    img.setImageAndDecodeBounds(ResourceTable.Media_rhby);
                }
                //返回false 这个事件只会触发一次 ,返回true 这个事件一直有效
                return true;
            }
        });

    }
}
